﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace BoldBrick.SharePoint.CommonControls.DataObjects.Base
{
	public abstract class BaseView : BaseDataObject
	{
		#region ------ Properties -----------------------------------------------------------------

		/// <summary>
		/// Gets true is the view is default view of the list
		/// </summary>
		public bool IsDefault { get; protected set; }

		/// <summary>
		/// Gets row limit of the view
		/// </summary>
		public uint RowLimit { get; protected set; }

		/// <summary>
		/// Gets true if view is paged, otherwise returns false
		/// </summary>
		public bool Paged { get; protected set; }

		/// <summary>
		/// Gets link to XSL file for view
		/// </summary>
		public string XslLink { get; protected set; }

		/// <summary>
		/// Gets content type ID in which can be this view visible
		/// </summary>
		public SPContentTypeId? ContentTypeId { get; protected set; }

		/// <summary>
		/// Gets true if this view is default for set content type
		/// </summary>
		public bool DefaultViewForContentType { get; protected set; }

		/// <summary>
		/// Gets query of this view with OrderBy and Where parts
		/// </summary>
		public string Query { get; protected set; }

		#endregion

		protected BaseView()
		{
			IsDefault = false;
			RowLimit = 100;
			Paged = true;
			DefaultViewForContentType = false;
		}

		#region ------ Metadata Methods -----------------------------------------------------------

		/// <summary>
		/// Gets view title
		/// </summary>
		/// <param name="language">Language of the list</param>
		/// <returns>Returns view title</returns>
		public virtual string GetTitle(uint language)
		{
			string source = string.Format("$Resources: {0}", GetTitleResourceKey());
			return SPUtility.GetLocalizedString(source, this.ResourceFileName, language);
		}

		/// <summary>
		/// Gets resource key for view title
		/// </summary>
		/// <returns>Returns resource key for view title</returns>
		protected virtual string GetTitleResourceKey()
		{
			throw new NotImplementedException("Implement 'GetTitleResourceKey' method or 'GetTitle' method in inherited class.");
		}
		
		/// <summary>
		/// Gets view page name without ".aspx" extension
		/// </summary>
		/// <param name="language">Language of the list</param>
		/// <returns>Returns page name without ".aspx" extension</returns>
		public virtual string GetPageName(uint language)
		{
			string source = string.Format("$Resources: {0}", GetPageNameResourceKey());
			return SPUtility.GetLocalizedString(source, this.ResourceFileName, language);
		}

		/// <summary>
		/// Gets resource key for title of the list
		/// </summary>
		/// <returns>Returns resource key for title of the list</returns>
		protected virtual string GetPageNameResourceKey()
		{
			throw new NotImplementedException("Implement 'GetPageNameResourceKey' method or 'GetPageName' method in inherited class.");
		}

		
		
		#endregion


		#region ------ Methods --------------------------------------------------------------------

		/// <summary>
		/// Gets <see cref="SPView"/> object based on this class
		/// </summary>
		/// <param name="web">Parent web of the list</param>
		/// <param name="list">The list that contains the view</param>
		/// <returns>Returns <see cref="SPView"/> object based on this class</returns>
		public SPView GetView(SPWeb web, SPList list)
		{
			string title = GetTitle(web.Language);
			SPView view = list.Views[title];
			return view;
		}

		/// <summary>
		/// Gets list of columns of the view
		/// </summary>
		/// <returns>Returns list of columns of the view</returns>
		public abstract List<ListColumn> GetViewColumns();


		#endregion

	}
}
